/*******************************************************************************
 * Copyright (c) 2006 Ultimate Technology, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bojan Vukojevic - initial API and implementation
 *******************************************************************************/
package com.ultimatetech.cim.views.content;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMMethod;
import javax.wbem.cim.CIMObjectPath;
import javax.wbem.client.CIMClient;

import org.eclipse.core.runtime.Preferences;

import com.ultimatetech.cim.preferences.CIMPreferencePage;

public class ClassInstances {

	CIMObjectPath cop = null;
	List instances;
	Preferences pref = null;
	boolean ino = false;
	public ClassInstances(CIMObjectPath c, Preferences p, boolean instanceNamesOnly){
		cop = c;
		pref = p;
		ino = instanceNamesOnly;
		initData();
	}
	
	private void initData() {
		CIMClient client = CIMConnect.connect(
				pref.getString(CIMPreferencePage.PreferenceNames.IS_SECURE),
				pref.getString(CIMPreferencePage.PreferenceNames.CIM_HOST),
				pref.getString(CIMPreferencePage.PreferenceNames.CIM_PORT),
				pref.getString(CIMPreferencePage.PreferenceNames.CIM_NAMESPACE)
				);
		try {
			if(!ino){
			Vector v = client.getClass(cop).getAllMethods();
			StringBuffer buf = new StringBuffer("Methods:\n");
			for(int i = 0; i < v.size(); i++){
				CIMMethod cm = (CIMMethod)v.get(i);
				buf.append(cm.toMOF());
			}
			Enumeration en = client.enumerateInstances(cop);
			instances = new ArrayList();
			instances.add(buf);
			while(en.hasMoreElements()){
				CIMInstance ci = (CIMInstance)en.nextElement();				
				instances.add(ci);
			}			
			} else {
				Enumeration en = client.enumerateInstanceNames(cop);
				instances = new ArrayList();
				while(en.hasMoreElements()){
					Object o = en.nextElement();				
					instances.add(o);
				}		
			}
			client.close();
		} catch (CIMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List getInstances(){
		return instances;
	}
	
	
	
}
